<template>
  <div class="app-container">
    <!-- 查询模块 -->
    <div class="formDiv">
      <el-form ref="form" :inline="true" :model="form" label-width="130px">
        <el-row>
          <el-col :span="6">
            <el-form-item>
              <el-button
                v-if="hasPermission('AdminAllowanceBlackListProvider.create')"
                type="primary"
                @click="addFn"
              >添加</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 添加弹窗 -->
    <el-dialog
      title="添加黑名单"
      :visible.sync="dialogVisible"
      :before-close="handleClose"
      width="468px"
    >
      <!-- 输入框 -->
      <el-form :inline="true" label-width="80px" label-position="right">
        <el-form-item label="电话">
          <el-input v-model="addForm.username"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="addForm.remark"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose()">取 消</el-button>
        <el-button type="primary" @click="addSubmit()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 主页列表 -->
    <div class="listDiv" v-loading="tableLoading">
      <!-- table列表 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="realname" label="姓名"></el-table-column>
        <el-table-column prop="username" label="电话">
          <template slot-scope="scope">
            <div>{{scope.row.username | phonereg }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注"></el-table-column>
        <el-table-column prop="admin" label="操作人"></el-table-column>
        <el-table-column prop="updatedAt" label="操作时间"></el-table-column>
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button
              v-if="hasPermission('AdminAllowanceBlackListProvider.delete')"
              type="text"
              size="small"
              @click="deleteList(scope.row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagesCon">
        <div>
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="listForm.pageNum"
            :page-sizes="page_sizes"
            :page-size="listForm.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page_totals"
          ></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { allowanceBlackList, create, deleteFn } from "@/api/blacklist";

export default {
  name: "blacklistIndex",
  data() {
    return {
      val: "1",
      dates: "",
      dialogVisible: false,
      tableLoading: true,
      form: {
        pageNum: 1,
        pageSize: 10
      },
      addForm: {},
      tableData: [],
      roleOptions: [],
      page_sizes: [10, 30, 50, 100],
      page_totals: 60,
      listForm: {
        pageNum: 1,
        pageSize: 10
      }
    };
  },
  filters: {
    cardType: function(e) {
      return window.helper.cardType(String(e));
    }
  },
  inject: ["reload"],
  mounted() {
    this.getList(this.listForm);
    this.roleOptions = window.helper.roleFn("listReturn");
  },
  methods: {
    //按钮权限
    hasPermission(e) {
      return window.helper.hasPermission(e);
    },
    // 删除
    deleteList(e) {
      let datas = { id: e };
      console.log(datas);
      this.$confirm("删除该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          deleteFn(datas).then(res => {
            if (!res.data.code) {
              this.succStatus("删除");
              this.reload();
            } else {
              this.$message.error(res.data.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //添加方法
    addFn() {
      this.addForm.username = "";
      this.addForm.remark = "";
      this.dialogVisible = true;
    },
    //弹窗关闭
    handleClose() {
      this.dialogVisible = false;
    },
    // 添加提交
    addSubmit() {
      // console.log(this.addForm)
      let datas = this.addForm;
      create(datas)
        .then(res => {
          console.log(res);
          if (!res.data.code) {
            this.succStatus("添加");
          } else {
            this.$message.error(res.data.msg);
          }
          this.reload();
        })
        .catch(error => {
          this.$message.error(error);
          this.reload();
        });
      this.dialogVisible = false;
    },
    //成功状态弹窗
    succStatus(txt) {
      this.$message({
        message: txt + "成功",
        type: "success"
      });
    },
    //失败状态弹窗
    errorStatus(txt) {
      this.$message.error(txt + "失败");
    },
    //列表数据获取
    getList(e) {
      this.tableLoading = true;
      let datas = e;
      allowanceBlackList(datas)
        .then(res => {
          if (!res.data.code) {
            // console.log(res)
            this.tableData = res.data.data.list;
            this.page_totals = res.data.data.count;
          } else {
            this.$message.error(res.data.msg);
          }
          this.tableLoading = false;
        })
        .catch(error => {
          this.$message.error(error);
        });
    },
    //改变页面条数
    handleSizeChange(val) {
      this.form.pageSize = val;
      this.getList(this.form);
    },
    //选择页数
    handleCurrentChange(val) {
      this.form.pageNum = val;
      this.getList(this.form);
    }
  }
};
</script>
<style scoped>
/* 分页 */
.pagesCon {
  height: 40px;
  margin-top: 20px;
}
.pagesCon > div {
  float: right;
}
</style>
